package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import connection.ServiceProxy;
import bean.*;
/**
 * Servlet implementation class ViewCart
 */
public class ViewCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		getUserCart(request,response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		getUserCart(request,response);
	}
	
	private void getUserCart(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
		
		String message = null; 
		HttpSession session = request.getSession();
		String userId = (String)session.getAttribute("userId");
		ServiceProxy oProxy = new ServiceProxy();
		CartItem aoCartItem[] = oProxy.getUserCart(userId);
		if(null == aoCartItem) {
			System.out.println("No adds");
			message = "Cart is empty";
			request.setAttribute("message", message);
		}
		else {
		
			request.setAttribute("CartItem", aoCartItem);
			
			Advertisement aoAdvert[] = oProxy.getUserCartItemDetails(userId);
			request.setAttribute("Advertisement", aoAdvert);			
		}
		
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/viewCart.jsp");
		rd.forward(request, response);
		
		
	}

}
